package com.tuoyu.peopletest.view.pop;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lxj.xpopup.core.CenterPopupView;
import com.tuoyu.peopletest.R;
import com.tuoyu.peopletest.scinterface.OnDataListener;
import com.tuoyu.peopletest.utils.SCUIUtils;
import com.tuoyu.peopletest.utils.SCUtils;

import androidx.annotation.NonNull;

/**
 * 带输入的dialog
 */
public class InputDialog extends CenterPopupView implements View.OnClickListener {

    private int from_type;
    private Object title;
    private Object inputHint;
    private Object inputText;
    private EditText etInput;
    private OnDataListener<String> listener;

    public InputDialog(@NonNull Context context, Object title, Object inputHint) {
        super(context);
        this.title = title;
        this.inputHint = inputHint;
    }

    public InputDialog(@NonNull Context context, Object title, Object inputHint, Object text, OnDataListener<String> listener) {
        super(context);
        this.title = title;
        this.inputHint = inputHint;
        this.inputText = text;
        this.listener = listener;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_input;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        TextView tvTitle = findViewById(R.id.tv_title);
        etInput = findViewById(R.id.et_input);
        SCUIUtils.setTextStr(tvTitle, title);
        if (inputHint != null) {
            SCUIUtils.setTextHintStr(etInput, inputHint);
        }

        findViewById(R.id.tv_btn_cancle).setOnClickListener(this);
        findViewById(R.id.tv_btn_sure).setOnClickListener(this);
    }

    @Override
    protected void onShow() {
        super.onShow();
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_btn_cancle:
                dismiss();
                break;
            case R.id.tv_btn_sure:

                String input = etInput.getText().toString();
                if (!TextUtils.isEmpty(input) && listener != null) {
                    listener.onData(input);

                }
                dismiss();
                break;
        }
    }

    public void setEdStr(String nickname) {
        etInput.setText(SCUtils.getNoEmpty(nickname));
        if(!TextUtils.isEmpty(nickname)){
            etInput.setSelection(nickname.length());
        }

    }


    public void setDataListener(OnDataListener<String> listener) {
        this.listener = listener;
    }

}
